﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace CPPEI.WPF
{
    public class CutInEffect : SwitchEffect
    {
        public override ModelVisual3D BuildIn(MeshGeometry3D geometry, View inView, System.Windows.Media.Animation.Storyboard storyboard3D, double durationTicks)
        {
            DiffuseMaterial material = new DiffuseMaterial(new ImageBrush(inView.Snapshot));

            ModelVisual3D mv3d = new ModelVisual3D();

            GeometryModel3D v3d = new GeometryModel3D();
            v3d.Geometry = geometry;
            v3d.Material = material;
            v3d.BackMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
            mv3d.Content = v3d;

            Transform3DGroup tf3dGroup = new Transform3DGroup();
            TranslateTransform3D ttf3d = new TranslateTransform3D(0, 0, 0);
            RotateTransform3D rtf3d = new RotateTransform3D();
            AxisAngleRotation3D aar3d = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 30);
            rtf3d.Rotation = aar3d;
            tf3dGroup.Children.Add(rtf3d);
            tf3dGroup.Children.Add(ttf3d);
            mv3d.Transform = tf3dGroup;

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 30;
            animation.To = 0;
            animation.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(durationTicks));
            Storyboard.SetTarget(animation, mv3d);
            Storyboard.SetTargetProperty(animation, new System.Windows.PropertyPath("(ModelVisual3D.Transform).(Transform3DGroup.Children)[0].(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"));
            storyboard3D.Children.Add(animation);

            DoubleAnimation animationMoveIn = new DoubleAnimation();
            animationMoveIn.From = inView.ActualWidth / 2;
            animationMoveIn.To = 0;
            animationMoveIn.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(durationTicks));
            Storyboard.SetTarget(animationMoveIn, mv3d);
            Storyboard.SetTargetProperty(animationMoveIn, new System.Windows.PropertyPath("(ModelVisual3D.Transform).(Transform3DGroup.Children)[1].(TranslateTransform3D.OffsetX)"));
            storyboard3D.Children.Add(animationMoveIn);
            //storyboard3D.Begin(this);

            return mv3d;
        }

        public override ModelVisual3D BuildOut(MeshGeometry3D geometry, View outView, System.Windows.Media.Animation.Storyboard storyboard3D, double durationTicks)
        {
            DiffuseMaterial material = new DiffuseMaterial(new ImageBrush(outView.Snapshot));

            ModelVisual3D mv3d = new ModelVisual3D();

            GeometryModel3D v3d = new GeometryModel3D();
            v3d.Geometry = geometry;
            v3d.Material = material;
            v3d.BackMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
            mv3d.Content = v3d;

            Transform3DGroup tf3dGroup = new Transform3DGroup();
            TranslateTransform3D ttf3d = new TranslateTransform3D(0, 0, 0);
            RotateTransform3D rtf3d = new RotateTransform3D();
            AxisAngleRotation3D aar3d = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 30);
            rtf3d.Rotation = aar3d;
            tf3dGroup.Children.Add(rtf3d);
            tf3dGroup.Children.Add(ttf3d);
            mv3d.Transform = tf3dGroup;

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 0;
            animation.To = 30;
            animation.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(durationTicks));
            Storyboard.SetTarget(animation, mv3d);
            Storyboard.SetTargetProperty(animation, new System.Windows.PropertyPath("(ModelVisual3D.Transform).(Transform3DGroup.Children)[0].(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"));
            storyboard3D.Children.Add(animation);

            DoubleAnimation animationMoveIn = new DoubleAnimation();
            animationMoveIn.From = 0;
            animationMoveIn.To = -outView.ActualWidth / 2;
            animationMoveIn.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(durationTicks));
            Storyboard.SetTarget(animationMoveIn, mv3d);
            Storyboard.SetTargetProperty(animationMoveIn, new System.Windows.PropertyPath("(ModelVisual3D.Transform).(Transform3DGroup.Children)[1].(TranslateTransform3D.OffsetX)"));
            storyboard3D.Children.Add(animationMoveIn);
            //storyboard3D.Begin(this);

            return mv3d;
        }
    }
}
